<template>
	<view class="drawresult" v-if="showDrawInfo">
		<view v-if="driverinfo.status==1" class="drawresult_top">
			<uni-row :gutter="30">
				<uni-col v-for="(item,index) in prizelist" :key="item.id" :span="12">
					<view class="prize_box" @click="choicePrize(item.id)">
						<image class="prize_img" :src="picUrl+item.url" mode="aspectFill"></image>
						<view class="display_center">
							<uni-icons v-if="item.id==gifts_id" type="circle-filled" size="18"
								color="#00A0DC"></uni-icons>
							<uni-icons v-else type="circle" size="18" color="#000000"></uni-icons>
							<view class="prize_text">{{item.name}}</view>
						</view>
					</view>
				</uni-col>
			</uni-row>
		</view>
		<view v-if="driverinfo.status==1" class="address_info_box">
			<view class="f-13 c-666666">请填写正确的奖品邮寄信息，以便工作人员联系。</view>
			<view class="address_info">
				<view v-if="addrsinfo.id==0" class="group_public_btn_empty_red mt-15" @click="goEditAddr(1)">请添加邮寄地址
				</view>
				<view v-if="addrsinfo.id!=0" class="addr_box" @click="goAddrList(index)">
					<uni-icons class="addr_location_icon" type="location" size="24" color="#00A0DC"></uni-icons>
					<view class="flex-1 ml-5 mr-15">
						<view class="display_flex">
							<view class="display_flex flex-1">
								<view class="f-15">{{addrsinfo.name}}</view>
								<view class="f-15 c-666666">{{addrsinfo.mobile}}</view>
								<view v-if="addrsinfo.status==1" class="def_btn ml-5">默认</view>
							</view>
						</view>
						<view class="f-12 c-999999 mt-10">
							{{addrsinfo.area.province_name}}{{addrsinfo.area.city_name}}{{addrsinfo.area.area_name}}{{addrsinfo.address}}
						</view>
					</view>
					<uni-icons type="forward" size="22" color="#00A0DC"></uni-icons>
				</view>
			</view>
			<view class="group_public_btn_block mt-40" @click="submitPrizeAddress">提交信息</view>
		</view>
		<view v-if="driverinfo.status==2" class="drawresult_top">
			<view class="f-20 f-w">您的奖品</view>
			<view class="f-18 mt-10">{{gifts_name}}</view>
			<image class="prize_img" :src="picUrl+gifts_pic" mode="aspectFit"></image>
		</view>
		<view v-if="driverinfo.status==2" class="address_info_box">
			<view class="address_info_title f-w">邮寄信息</view>
			<view class="address_info">
				<view class="display_flex mt-20">
					<view class="f-13" style="width: 255rpx;">姓名</view>
					<view class="c-999999 f-12 flex-1 text-r">{{driverinfo.address_info.name}}</view>
				</view>
				<view class="display_flex mt-20">
					<view class="f-13" style="width: 255rpx;">手机号码</view>
					<view class="c-999999 f-12 flex-1 text-r">{{driverinfo.address_info.mobile}}</view>
				</view>
				<view class="display_flex mt-20">
					<view class="f-13" style="width: 255rpx;">详细地址</view>
					<view class="c-999999 f-12 flex-1 text-r">
						{{driverinfo.address_info.area.province_name}}{{driverinfo.address_info.area.city_name}}{{driverinfo.address_info.area.area_name}}{{driverinfo.address_info.address}}
					</view>
				</view>
			</view>
			<view class="address_info_title f-w mt-20">物流状态</view>
			<view class="address_info">
				<view class="display_flex mt-20">
					<view class="f-13" style="width: 255rpx;">发货信息</view>
					<view v-if="driverinfo.express_no" class="c-00A0DC f-12 flex-1 text-r">已发货</view>
					<view v-else class="c-red f-12 flex-1 text-r">未发货</view>
				</view>
				<view class="display_flex mt-20" v-if="driverinfo.express_no">
					<view class="f-13" style="width: 255rpx;">物流单号</view>
					<view class="c-999999 f-12 flex-1 text-r">{{driverinfo.express_no}}</view>
					<view class="copy_tag" @click="copyNum(driverinfo.express_no)">复制</view>
				</view>
			</view>
			<!-- <view class="group_public_btn_block mt-40" @click="editPrizeAddress">修改邮寄信息</view> -->
		</view>
	</view>
</template>

<script>
	import {
		getDefaultShopAddr,
		getHuodongDriverInfo,
		submitHuodongDriver
	} from '@/api/motortime.js';
	let app = getApp()
	export default {
		data() {
			return {
				addrsinfo: '',
				driverinfo: {},
				prizelist: [],
				gifts_id: '',
				showDrawInfo: false,
				isSbumit: true,
				gifts_name: '',
				gifts_pic: '',
				picUrl: app.globalData.picUrl,
				onlinePic: app.globalData.onlinePic,
			};
		},
		onShow() {
			if (!uni.getStorageSync('access_token')) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			} else {
				if (uni.getStorageSync('onload')) {
					uni.showLoading({})
					this.getHuodongDriverInfo()
					uni.removeStorage({
						key: 'onload'
					})
				}
			}
			if (uni.getStorageSync('taskAddrInfo')) {
				this.addrsinfo = uni.getStorageSync('taskAddrInfo') ? uni.getStorageSync('taskAddrInfo') : ''
				this.address_id = uni.getStorageSync('taskAddrInfo') ? uni.getStorageSync('taskAddrInfo').id : ''
				uni.removeStorage({
					key: 'taskAddrInfo'
				})
			} else {
				this.getDefaultShopAddr()
			}
		},
		onLoad: function(options) {
			this.id = options.drive_id
			this.channel = options.channel ? options.channel : 0
			if (uni.getStorageSync('access_token')) {
				uni.showLoading({})
				this.getHuodongDriverInfo()
			}
		},
		methods: {
			getHuodongDriverInfo() {
				getHuodongDriverInfo({
					id: this.id,
				}).then(res => {
					if (res.state == 1) {
						console.log(res)
						this.driverinfo = res.data.info
						this.isDrawPop = res.data.info.popup_status == 1 ? true : false
						var list = res.data.info.gifts_list
						if (list.length > 0) {
							var that = this
							list.forEach(function(item, index) {
								item.status = 0
								if (res.data.info.gifts_id == item.id) {
									that.gifts_name = item.name
									that.gifts_pic = item.url
								}
							});
							that.prizelist = list
						}
						this.showDrawInfo = true
						uni.hideLoading()
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			getDefaultShopAddr() {
				getDefaultShopAddr({}).then(res => {
					if (res.state == 1) {
						uni.hideLoading()
						this.addrsinfo = res.data
						this.address_id = res.data.id
					} else {
						uni.hideLoading()
						this.loadingTitle = "- 暂无更多 -"
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			goAddrList() {
				uni.navigateTo({
					url: '/pages/my/myaddress'
				})
			},
			goEditAddr(type, index) {
				if (type == 2) {
					uni.setStorage({
						key: 'taskAddrInfo',
						data: this.addrsList[index]
					})
				}
				uni.navigateTo({
					url: '/pages/my/editaddress'
				})
			},
			editPrizeAddress() {

			},
			submitPrizeAddress() {
				var that = this
				if (that.gifts_id == '') {
					uni.showToast({
						title: '请选择奖品',
						mask: true,
						icon: 'none',
						duration: 1500
					})
					return false;
				} else if (that.address_id == '') {
					uni.showToast({
						title: '请选择奖品邮寄地址',
						mask: true,
						icon: 'none',
						duration: 1500
					})
					return false;
				}
				console.log({
					gifts_id: that.gifts_id,
					address_id: that.address_id,
				})
				if (that.isSbumit) {
					that.isSbumit = false
					uni.showModal({
						content: '提交后不可修改，确认提交吗？',
						confirmText: '确认',
						confirmColor: '#00A0DC',
						success: function(res) {
							if (res.confirm) {
								uni.showLoading({})
								submitHuodongDriver({
									gifts_id: that.gifts_id,
									address_id: that.address_id,
								}).then(res => {
									if (res.state == 1) {
										that.getHuodongDriverInfo()
										that.isSbumit = true
									} else {
										uni.hideLoading()
										that.isSbumit = true
										uni.showToast({
											title: res.error[0],
											mask: true,
											icon: 'none',
											duration: 1500
										})
									}
								})
							} else if (res.cancel) {
								that.isSbumit = true
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			choicePrize(id) {
				this.gifts_id = id
			},
			copyNum(text) {
				uni.setClipboardData({
					data: text,
					success: function() {
						uni.showToast({
							title: '复制成功',
							mask: true,
							icon: 'success',
							duration: 1500

						})
					}
				});
			},
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
			this.getHuodongDriverInfo()
		}
	}
</script>

<style lang="scss">
	.drawresult {
		.drawresult_top {
			padding: 60rpx 30rpx 40rpx 30rpx;
			background-color: #FAFAFA;
			text-align: center;

			.prize_box {
				margin-bottom: 30rpx;

				.prize_img {
					width: 306rpx;
					height: 180rpx;
					border-radius: 16rpx;
				}

				.prize_text {
					font-size: 28rpx;
					font-weight: bold;
					text-align: center;
					margin-left: 10rpx;
				}

			}
		}

		.prize_img {
			width: 306rpx;
			height: 180rpx;
			margin-top: 40rpx;
		}

		.addressform_box {
			padding: 38rpx 60rpx;

			.addressform_cell {
				margin-top: 40rpx;
				display: flex;
				align-items: center;

				.addressform_lable {
					width: 110rpx;
					font-size: 26rpx;
					text-align: left;
					margin-right: 20rpx;
				}

				.addressform_input_box {
					flex: 1;
					padding: 20rpx 30rpx;
					border-radius: 8rpx;
					border: 1rpx solid #E9E9E9;

					.addressform_input {
						font-size: 26rpx;
					}
				}

				.addressform_textarea_box {
					flex: 1;
					padding: 30rpx 20rpx;
					border-radius: 8rpx;
					border: 1rpx solid #E9E9E9;

					.addressform_textarea {
						width: 100%;
						font-size: 26rpx;
					}
				}
			}
		}

		.address_info_box {
			padding: 38rpx 60rpx;

			.address_info_title {
				font-size: 32rpx;
				padding-bottom: 15rpx;
				border-bottom: 1rpx solid #E9E9E9;
			}

			.address_info {
				// padding: 0 12rpx;
			}

			.step_select {
				.uni-select {
					font-size: 26rpx;
					height: 86rpx !important;
				}
			}

			.copy_tag {
				width: 60rpx;
				height: 30rpx;
				font-size: 22rpx;
				border-radius: 4rpx;
				background: #C5C5C5;
				opacity: 0.6;
				text-align: center;
				margin-left: 16rpx;
			}

			.addr_box {
				display: flex;
				align-items: center;
				padding: 40rpx 0;

				.addr_location_icon {
					margin-left: -10rpx;
				}

				.def_btn {
					color: #00A0DC;
					font-size: 20rpx;
					padding: 0rpx 10rpx;
					border-radius: 8rpx;
					border: 1rpx solid #00A0DC;
				}
			}
		}

		.ticket_pic {
			width: 240rpx;
			height: 240rpx;
			vertical-align: middle;
		}

		.download_qrcode_pic {
			width: 52rpx;
			height: 52rpx;
			vertical-align: middle;
		}
	}
</style>